<template>
  <div>
    <van-nav-bar title="登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' },{pattern:/\w{5,}/,message:'长度必须是5个字符以上'}]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' },{pattern:/\w{6,}/,message:'长度必须是6个字符以上'}]"
      />
      <div style="margin: 16px">
        <van-button block type="primary" native-type="submit" class="login-btn">登录</van-button>
      </div>
    </van-form>
    <div class="link">
      <router-link to="/register">去注册</router-link>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'LoginPage',
  data () {
    return {
      username: '',
      password: '',
      value: ''
    }
  },
  methods: {
    async onSubmit (values) {
      const res = await login(values)
      console.log(res)
      this.$toast.success('登录成功')
      setToken(res.data.token)
      this.$router.push('/article')
    }
  }
}
</script>

<style scoped lang="less">
.link {
  text-align: right;
  margin: 16px;
}
.login-btn{
  background-color: #fa6d1d;
  border-color:#fa6d1d;
}

/deep/ .van-field__control{
  color:red !important;
}
</style>

// 1. 接口相关的所有的 比如参数名字
// 2. 只要是第二次写的都要CV
